<template>
  <div class="GWarningSet">
    <zyInfoPanel title="寄件预警设置" :showUnfoldButton="false" class="warnItem">
      <div class="container">
        <div class="form">
          <div class="form-item">
            <div class="form-item-name">时间范围（天数）</div>
            <zy-input placeholder="请输入时间范围（天数）" v-model="configs[0].config_value"></zy-input>
          </div>
          <div class="form-item">
            <div class="form-item-name">寄件数量阈值</div>
            <zy-input placeholder="请输入寄件数量阈值" v-model="configs[1].config_value"></zy-input>
          </div>
          <div class="form-item btnArea">
            <zy-button @click="reset('1')">重置</zy-button>
            <zy-button @click="save('1')">保存</zy-button>
          </div>
        </div>
      </div>
    </zyInfoPanel>
    <zyInfoPanel title="收件预警设置" :showUnfoldButton="false" class="warnItem">
      <div class="container">
        <div class="form">
          <div class="form-item">
            <div class="form-item-name">时间范围（天数）</div>
            <zy-input placeholder="请输入时间范围（天数）" v-model="configs[2].config_value"></zy-input>
          </div>
          <div class="form-item">
            <div class="form-item-name">收件数量阈值</div>
            <zy-input placeholder="请输入收件数量阈值" v-model="configs[3].config_value"></zy-input>
          </div>
          <div class="form-item btnArea">
            <zy-button @click="reset('2')">重置</zy-button>
            <zy-button @click="save('2')">保存</zy-button>
          </div>
        </div>
      </div>
    </zyInfoPanel>
    <zyInfoPanel title="收件预警设置" :showUnfoldButton="false" class="warnItem">
      <div class="container">
        <div class="pageTitle">
          <div class="title">研判权重设置</div>
        </div>
        <div class="form">
          <div class="form-item">
            <div class="form-item-name">高危人员权重</div>
            <zy-input type="rate" placeholder="请输入高危人员权重" v-model="form.subsidiary1"></zy-input>
          </div>
          <div class="form-item">
            <div class="form-item-name">高危物品权重</div>
            <zy-input type="rate" placeholder="请输入高危物品权重" v-model="form.subsidiary2"></zy-input>
          </div>
          <div class="form-item">
            <div class="form-item-name">高危线路权重</div>
            <zy-input type="rate" placeholder="请输入高危线路权重" v-model="form.subsidiary3"></zy-input>
          </div>
          <div class="form-item">
            <div class="form-item-name">高危电话权重</div>
            <zy-input type="rate" placeholder="请输入高危电话权重" v-model="form.subsidiary4"></zy-input>
          </div>
          <div class="form-item">
            <div class="form-item-name">高危行为权重</div>
            <zy-input type="rate" placeholder="请输入高危行为权重" v-model="form.subsidiary5"></zy-input>
          </div>
        </div>
        <div class="tips">注：五项权重设置之和不能超过100%。</div>
      </div>
      <div class="container" :loading="loading">
        <div class="pageTitle">
          <div class="title">研判结果设置</div>
          <zy-button @click="addTr" :disabled="parseFloat(list[list.length - 1].end_score)>=100">加行</zy-button>
        </div>
        <div class="head tr">
          <div class="index">序号</div>
          <div class="interval">分值区间</div>
          <div class="color">颜色标记</div>
          <div class="dubious">是否标记可疑件</div>
          <div class="early_type">预警方式</div>
          <div class="police_ids">预警人员</div>
          <div class="btnArea">操作</div>
        </div>
        <div class="tr" v-for="(item,index) in list" :key="index">
          <div class="index">{{index+1}}</div>
          <div class="interval">
            <zy-input v-model="item.start_score" size="mini" type="rate" disabled></zy-input>
            <span class="unit"> ≤ X < </span>
            <zy-input v-model="item.end_score" size="mini" type="rate"
                      :disabled="index < list.length-1"></zy-input>
            <span class="unit">分</span>
          </div>
          <div class="color">
            <el-color-picker v-model="item.color" :disabled="index < list.length-1"></el-color-picker>
          </div>
          <div class="dubious">
            <el-select v-model="item.is_dubious" placeholder="请选择" :disabled="index < list.length-1">
              <el-option v-for="option in optionList" :key="option.value" :label="option.label"
                         :value="option.value"></el-option>
            </el-select>
          </div>
          <div class="early_type">
            <el-checkbox-group v-model="item.early_type" :disabled="index < list.length-1">
              <el-checkbox label="1">站内信</el-checkbox>
              <el-checkbox label="2">短信</el-checkbox>
            </el-checkbox-group>
          </div>
          <div class="police_ids">
            <el-select v-model="item.police_ids" placeholder="请选择" type="selectMultiple" filterable multiple
                       :disabled="index < list.length-1">
              <el-option v-for="option in policeList" :key="option.value" :label="option.label"
                         :value="option.value"></el-option>
            </el-select>
          </div>
          <div class="btnArea">
            <zy-button text-button mini v-if="index==list.length-1" @click="clear">清空</zy-button>
            <zy-button text-button mini type="danger" class="delBtn" v-if="index==list.length-1"
                       @click="deleted(index)">删除
            </zy-button>
          </div>
        </div>
        <div class="btn-box">
          <zy-button @click="reset('3')">重置</zy-button>
          <zy-button @click="submit" type="primary">提交</zy-button>
        </div>
      </div>
    </zyInfoPanel>
  </div>
</template>

<script>
	export default {
		name: "GWarningSet",
		props: {},
		data() {
			return {
				configs: [
					{
						channel: 2,
						config_code: "judge_datediff",
						config_id: 3,
						config_name: "时间范围(天数)",
						config_value: "",
						input_range: null,
						input_type: "text",
						remark: "",
					},
					{
						channel: 2,
						config_code: "judge_number",
						config_id: 4,
						config_name: "寄件数量阈值",
						config_value: "",
						input_range: "",
						input_type: "text",
						remark: "",
					},
					{
						channel: 2,
						config_code: "judge_datediff_receive",
						config_id: 5,
						config_name: "时间范围(天数)",
						config_value: "",
						input_range: null,
						input_type: "text",
						remark: "",
					},
					{
						channel: 2,
						config_code: "judge_number_receive",
						config_id: 6,
						config_name: "收件数量阈值",
						config_value: "",
						input_range: "",
						input_type: "text",
						remark: "",
					}
				],
				form: {
					subsidiary1: '',
					subsidiary2: '',
					subsidiary3: '',
					subsidiary4: '',
					subsidiary5: '',
				},
				loading: false,
				list: [
					{
						start_score: 0,
						end_score: '',
						color: '',
						is_dubious: '',
						early_type: [],
						police_ids: []
					}
				],
				optionList: [
					{label: '是', value: '1'},
					{label: '否', value: '0'},
				],
				policeList: [],
			};
		},
		computed: {},
		watch: {},
		components: {},
		methods: {
			getData() {
				this.$axios.post(this.$api.g_getresearchJudge, {}).then(res => {
					let {code, msg, data} = res;
					if (code == 200) {
						this.form = {
							subsidiary1: data.dict?data.dict.subsidiary1 : '',
							subsidiary2: data.dict?data.dict.subsidiary2 : '',
							subsidiary3: data.dict?data.dict.subsidiary3 : '',
							subsidiary4: data.dict?data.dict.subsidiary4 : '',
							subsidiary5: data.dict?data.dict.subsidiary5 : '',
						}
						this.list = data.list && data.list.length ? data.list.filter(item => {
							item.early_type = item.early_type.split(',') || '';
							item.police_ids = item.police_ids.split(',') || ''
							item.is_dubious = String(item.is_dubious)
							return item;
						}) : [{
							start_score: 0,
							end_score: '',
							color: '',
							is_dubious: '',
							early_type: [],
							police_ids: []
						}]
					} else {
						this.$message.error(msg);
					}
				}).catch(ret => {
					this.$message.error(ret.msg || '服务繁忙，请稍后再试');
				});
			},
			getConfigData() {
				this.$axios.post(this.$api.admin_getChannelConfig, {
					channel: 2
				}).then(res => {
					let {code, msg, data} = res;
					if (code == 200) {
						this.configs.forEach(item => {
							for (let val of data) {
								if (item.config_code === val.config_code) {
									item.config_value = val.config_value
								}
							}
						})
					} else {
						this.$message.error(msg);
					}
				}).catch(ret => {
					this.$message.error(ret.msg || '服务繁忙，请稍后再试');
				});
			},
			reset(type) {
				switch (type) {
					case '1':
						this.configs[0].config_value = '';
						this.configs[1].config_value = ''
						break;
					case '2':
						this.configs[2].config_value = '';
						this.configs[3].config_value = ''
						break;
					case '3':
						this.form = {
							subsidiary1: '',
							subsidiary2: '',
							subsidiary3: '',
							subsidiary4: '',
							subsidiary5: '',
						}
						this.list = [{
							start_score: 0,
							end_score: '',
							color: '',
							is_dubious: '',
							early_type: [],
							police_ids: []
						}]
						break;
				}
			},
			save(type) {
				let configs = [];
				switch (type) {
					case '1':
						configs = JSON.parse(JSON.stringify(this.configs)).splice(0, 2);
						this.setConfig(configs)
						break;
					case '2':
						configs = JSON.parse(JSON.stringify(this.configs)).splice(2, 2);
						this.setConfig(configs)
						break;
				}
			},
			setConfig(configs) {
				this.$axios.post(this.$api.admin_setConfig, {
					configs
				}).then(res => {
					let {code, msg, data} = res;
					if (code == 200) {
						this.$message.success('设置成功!')
					} else {
						this.$message.error(msg);
					}
				}).catch(ret => {
					this.$message.error(ret.msg || '服务繁忙，请稍后再试');
				});
			},
			submit() {
				let subsidiary = 0, postData = {...this.form}, list = JSON.parse(JSON.stringify(this.list)),
					item = list[list.length - 1];
				if (!item.end_score.length || parseFloat(item.end_score) < parseFloat(item.start_score))
					return this.$message.error('请重新设置分值区间')
				if (!item.color.length)
					return this.$message.error('请选择标记颜色')
				if (!item.is_dubious)
					return this.$message.error('请选择是否标记可疑件')
				for (let key in this.form) {
					subsidiary += parseFloat(this.form[key])
				}
				if (!this.form.subsidiary1) {
					return this.$message.error('请输入高危人员权重')
				}
				if (!this.form.subsidiary2) {
					return this.$message.error('请输入高危物品权重')
				}
				if (!this.form.subsidiary3) {
					return this.$message.error('请输入高危线路权重')
				}
				if (!this.form.subsidiary4) {
					return this.$message.error('请输入高危电话权重')
				}
				if (!this.form.subsidiary5) {
					return this.$message.error('请输入高危行为权重')
				}
				if (subsidiary > 100) {
					return this.$message.error('综合研判五项权重之和不可超过100%')
				}
				if (parseFloat(this.list[this.list.length - 1].end_score) < 100) {
					return this.$message.error('请加行设置研判结果')
				}
				list.forEach(item => {
					item.early_type = item.early_type.length ? item.early_type.join() : ''
					item.police_ids = item.police_ids.length ? item.police_ids.join() : ''
				})
				postData['research_judge_list'] = list;
				console.log(postData)
				this.$axios.post(this.$api.g_researchJudgeAdd, {...postData}).then(res => {
					let {code, msg, data} = res;
					if (code == 200) {
						this.$message.success('提交成功!')
					} else {
						this.$message.error(msg);
					}
				}).catch(ret => {
					this.$message.error(ret.msg || '服务繁忙，请稍后再试');
				});
			},
			getPoliceData() {
				this.$axios.post(this.$api.g_listContactPolice, {
					pagination: {
						page_start: 1,
						page_size: 9999
					}
				}).then(res => {
					let {code, msg, data} = res;
					if (code == 200) {
						this.policeList = data.list.filter(item => {
							item.value = String(item.id);
							item.label = item.name
							return item;
						})
					} else {
						this.$message.error(msg);
					}
				}).catch(ret => {
					this.$message.error(ret.msg || '服务繁忙，请稍后再试');
				});
			},
			addTr() {
				let list = JSON.parse(JSON.stringify(this.list)),
					item = list[list.length - 1];
				if (!item.end_score.length || parseFloat(item.end_score) < parseFloat(item.start_score))
					return this.$message.error('请重新设置分值区间')
				if (!item.color.length)
					return this.$message.error('请选择标记颜色')
				if (!item.is_dubious)
					return this.$message.error('请选择是否标记可疑件')
				this.list.push({
					start_score: list[list.length - 1].end_score,
					end_score: '',
					color: '',
					is_dubious: '',
					early_type: [],
					police_ids: []
				})
			},
			deleted(index) {
				if (index > 0)
					this.list.splice(index, 1)
			},
			clear() {
				let list = JSON.parse(JSON.stringify(this.list))
				if (this.list.length > 1) {
					this.list = list.filter((item, index) => {
						if (index == list.length - 1) {
							item.start_score = list[list.length - 2].end_score || 0;
							item.end_score = ''
							item.color = '';
							item.is_dubious = ''
							item.early_type = []
							item.police_ids = []
						}
						return item;
					})
				} else {
					this.list = [{
						start_score: 0,
						end_score: '',
						color: '',
						is_dubious: '',
						early_type: [],
						police_ids: []
					}]
				}
			}
		},
		mounted() {
			this.getConfigData();
			this.getData();
			this.getPoliceData()
		},
		created() {
		}
	}
</script>

<style scoped lang="less" src="@/style/government/systemManage/warningSet/index.less">

</style>
